package com.qf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.*;
import com.qf.service.*;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/23 20:44
 */

@RestController
@RequestMapping("/admin")
@Slf4j
@CrossOrigin
public class MarketController {
    @Autowired
    private DtsRegionService dtsRegionService;
    @Autowired
    private DtsBrandService dtsBrandService;
    @Autowired
    private DtsAdminService dtsAdminService;
    @Autowired
    private DtsCategoryService categoryService;
    @Autowired
    private DtsGoodsService dtsGoodsService;

    //http://localhost:8080/admin/region/list?page=1&limit=20&name=a&code=d
    @RequiresPermissionsDesc(menu = {"商场管理","关键词"},button ="查询")
    @RequiresPermissions("admin:region:list")
    @GetMapping("/region/list")
    public Object regionList(Integer page, Integer limit, String sort, String order, String name, Integer code) {
        Page<DtsRegion> dtsRegionPage = dtsRegionService.selectPage(page, limit, sort, order, name, code);
        List<DtsRegion> records = dtsRegionPage.getRecords();
        long total = dtsRegionPage.getTotal();

        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", records);
        return ResponseUtil.ok(data);
    }

    //ttp://localhost:8080/admin/brand/list?page=1&limit=20&id=xx&name=xd&sort=add_time&order=de
    @RequiresPermissionsDesc(menu = {"商场管理","品牌制造商"},button ="查找")
    @RequiresPermissions("admin:brand:list")
    @GetMapping("/brand/list")
    public Object brandList(Integer page, Integer limit, String sort, String order, String name, Integer id) {
        Page<DtsBrand> dtsBrandPage = dtsBrandService.selectPage(page, limit, sort, order, name, id);
        long total = dtsBrandPage.getTotal();
        List<DtsBrand> records = dtsBrandPage.getRecords();
        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", records);
        return ResponseUtil.ok(data);
    }

    //http://localhost:8083/admin/brand/catAndAdmin

    @GetMapping("/brand/catAndAdmin")
    public Object catAndAdmin() {
        Set<CatVo> adminList = dtsAdminService.selectAdminList();
        Set<CatVo> categoryList = categoryService.selectCategoryAllList();
        HashMap<String, Object> data = new HashMap<>();
        data.put("adminList", adminList);
        data.put("categoryList", categoryList);
        return ResponseUtil.ok(data);

    }

    //http://localhost:8080/admin/brand/create
    @RequiresPermissionsDesc(menu = {"商场管理","品牌制造商"},button ="添加")
    @RequiresPermissions("admin:brand:create")
    @PostMapping("/brand/create")
    public Object brandCreate(@RequestBody DtsBrand dtsBrand) {
        if(dtsBrand==null){
            return  ResponseUtil.badArgumentValue();
        }
        dtsBrand.setAddTime(new Date());
        dtsBrand.setUpdateTime(new Date());
        dtsBrand.setDeleted(false);
        dtsBrandService.insert(dtsBrand);
        //   DtsGoods dtsGoods = dtsGoodsService.selectByBrandId(dtsBrand.getDefaultCategoryId());
        return ResponseUtil.ok(dtsBrand);
    }
    @RequiresPermissionsDesc(menu = {"商场管理","品牌制造商"},button ="编辑")
    @RequiresPermissions("admin:brand:update")
    @PostMapping("/brand/update")
    public Object brandUpdate(@RequestBody DtsBrand dtsBrand) {
        if(dtsBrand==null){
            return  ResponseUtil.badArgumentValue();
        }
        dtsBrand.setUpdateTime(new Date());
        dtsBrandService.update(dtsBrand);
        return ResponseUtil.ok(dtsBrand);
    }
    @RequiresPermissionsDesc(menu = {"商场管理","品牌制造商"},button ="删除")
    @RequiresPermissions("admin:brand:delete")
    @PostMapping("/brand/delete")
    public Object brandDelete(@RequestBody DtsBrand dtsBrand) {
        if(dtsBrand==null){
            return  ResponseUtil.badArgumentValue();
        }
        dtsBrand.setDeleted(true);
        dtsBrand.setUpdateTime(new Date());
        dtsBrandService.deleteBrand(dtsBrand);
        return ResponseUtil.ok();
    }


}
